<template>
	<view>
		<!-- #ifdef H5 -->
		<view id="captcha"></view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		captchaId
	} from '@/config/index.js'
	//#ifdef H5
	import initNECaptchaWithFallback from './h5SDK.js'
	//#endif
	export default {
		data() {
			return {
				instance: null
			}
		},
		created() {
			this.init()
		},
		methods: {
			init() {
				// #ifdef H5
				let _this = this;
				initNECaptchaWithFallback({
						captchaId,
						protocol: 'https',
						element: "#captcha",
						mode: "popup",
						width: '300px',
						feedbackEnable:false,
						apiVersion: 2,
						popupStyles: {
							position: 'fixed',
							top: '30%',
							
						},
						onVerify(err,data){
							if(err){
								_this.$emit('error',err.message)
							}else{
								_this.$emit('success',data.validate)
							}
						}
					},
					(instance) => {
						_this.instance = instance;
						_this.$emit('load')
					},
					(err) => {
						uni.showToast({
							icon:'none',
							title:"行为验证初始化失败，请联系客服"
						})
						_this.$emit('error', err);
					}
				)
				// #endif
				// #ifdef APP
				try {
					this.instance = uni.requireNativePlugin('YD-Captcha')
					// console.log(this.instance)
					this.instance.init({
						captcha_id: captchaId,
						is_no_sense_mode: false
					})
					this.$emit('load')
				} catch (err) {
					console.log(err)
					uni.showToast({
						icon:'none',
						title:"行为验证初始化失败，请联系客服"
					})
					this.$emit('error', err);
				}
				// #endif
			},
			showCaptcha() {
				// #ifdef H5
				this.instance.refresh();
				this.instance.verify();
				// #endif
				// #ifdef APP
				this.instance.showCaptcha((data) => {
					let {method}=data;
					if (method == "onSuccess") {
						// 验证成功
						var {validate} = data.data;
						this.$emit('success',validate)
					} else if (method == "onError") {
						var data = data.data;
						this.$emit('error',data.message)
					}
				})
				// #endif
			},
		}
	}
</script>

<style>

</style>
